package com.wxzl.community.common.base

import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.text.Html
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.text.HtmlCompat
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.wxzl.community.common.utils.ext.log
import com.wxzl.community.common.utils.ext.setPath
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Matcher
import java.util.regex.Pattern

object BindingAdapterUtils {
    private val TAG = BindingAdapterUtils::class.qualifiedName

    @JvmStatic
    @BindingAdapter("loadText")
    fun loadText(textview: TextView, content: String?) {

        content?.let {
            textview.text = getHTMLStr(content)
        }
    }


    @JvmStatic
    @BindingAdapter("loadhtml")
    fun loadHtml(textview: TextView, content: String?) {

        content?.let {

            textview.text = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                Html.fromHtml(content, HtmlCompat.FROM_HTML_MODE_LEGACY)
            } else {
                Html.fromHtml(content)
            }


        }
    }

    fun getHTMLStr(htmlStr: String): String? {

        //先将换行符保留，然后过滤标签
        var htmlStr: String? = htmlStr
        val p_enter: Pattern = Pattern.compile("<br/>", Pattern.CASE_INSENSITIVE)
        val m_enter: Matcher = p_enter.matcher(htmlStr)
        htmlStr = m_enter.replaceAll("\n")

        //过滤html标签
        val p_html: Pattern = Pattern.compile("<[^>]+>", Pattern.CASE_INSENSITIVE)
        val m_html: Matcher = p_html.matcher(htmlStr)
        return m_html.replaceAll("")
    }

    @JvmStatic
    @BindingAdapter("url")
    fun setImgURL(view: ImageView, url: String?) {
        url?.let {
            if (it.isNotEmpty()) {
                view.setPath(it)
            }


        }


    }

    @JvmStatic
    @BindingAdapter("icon")
    fun setImgURL(view: ImageView, resId: Int?) {

        resId?.let {

            view.setImageResource(it)
        }


    }


    @JvmStatic
    @BindingAdapter("text_int")
    fun setIntValue(view: EditText, v: Int?) {
        v?.let {
            view.setText(it.toString())
        }

    }

    @JvmStatic
    @BindingAdapter("text_int")
    fun setTextViewIntValue(view: TextView, v: Int?) {
        v?.let {
            view.text = it.toString()
        }

    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_int", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getIntValue(view: EditText): Int? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toInt()
        } catch (e: Exception) {
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_float")
    fun setFloatValue(view: EditText, v: Float?) {
        v?.let {
            view.setText(it.toString())
        }

    }

    @JvmStatic
    @BindingAdapter("text_float")
    fun setTextViewFloatValue(view: TextView, v: Float?) {
        v?.let {
            view.text = it.toString()
        }

    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_float", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getFloatValue(view: EditText): Float? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toFloat()
        } catch (e: Exception) {
            Log.e(TAG, "String转Float数据转换出错" + e.toString())
            throw e
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_long")
    fun setLongValue(view: EditText, v: Long?) {
        v?.let {
            view.setText(it.toString())
        }
    }

    @JvmStatic
    @BindingAdapter("text_long")
    fun setTextViewLongValue(view: TextView, v: Long?) {
        v?.let {
            view.text = it.toString()
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_long", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getLongValue(view: EditText): Long? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toLong()
        } catch (e: Exception) {
            throw e
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_double")
    fun setDoubleValue(view: EditText, v: Double?) {
        v?.let {
            view.setText(it.toString())
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_double", event = "android:textAttrChanged")
    @Throws(
            NumberFormatException::class
    )
    fun getDoubleValue(view: EditText): Double? {
        val string = view.text.toString()
        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            string.toDouble()
        } catch (e: Exception) {
            throw  e
        }
    }

    @JvmStatic
    @BindingAdapter("text_boolean")
    fun setBooleanValue(view: EditText, v: Boolean?) {
        v?.let {
            view.setText(if (it) "是" else "否")
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_boolean", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getBooleanValue(view: EditText): Boolean? {
        val string = view.text.toString()

        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            return string == "是"

        } catch (e: Exception) {
            throw e
            null
        }
    }

    @JvmStatic
    @BindingAdapter("text_date")
    fun setDateValue(view: EditText, v: Date?) {
        v?.let {
            view.setText(SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v))
        }
    }

    @JvmStatic
    @BindingAdapter("text_date")
    fun setTextViewDateValue(view: TextView, v: Date?) {
        v?.let {
            view.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v)
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_date", event = "android:textAttrChanged")
    @Throws(NumberFormatException::class)
    fun getDateValue(view: EditText): Date? {
        val string = view.text.toString()

        return if (TextUtils.isEmpty(string)) {
            null
        } else try {
            return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string)

        } catch (e: Exception) {
            throw e
            null
        }
    }


    @JvmStatic
    @BindingAdapter("text_string")
    fun setStringValue(view: EditText, v: String?) {
        v?.let {
            view.setText(it)
        }
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "text_string", event = "android:textAttrChanged")
    fun getStringValue(view: EditText): String? {
        val string = view.text.toString()

        return if (TextUtils.isEmpty(string)) "" else string
    }

    @JvmStatic
    @BindingAdapter("backgroundResource")
    fun setBackgroundValue(view: View, v: Int?) {
        v?.let {
            view.setBackgroundResource(it)
        }
    }

    @JvmStatic
    @BindingAdapter("textColorResource")
    fun setTextColorValue(view: TextView, v: Int?) {
        v?.let {
            try {
                view.setTextColor(ContextCompat.getColor(view.context, v))
            } catch (e: Resources.NotFoundException) {
                e.printStackTrace()
            }

        }
    }



    @JvmStatic
    @BindingAdapter("iconResource")
    fun setIconResourceValue(view: ImageView, v: Int?) {
        v?.let {
            view.setImageResource(it)
        }
    }

}